$(function () {
   $("body").on('click','.updateBinding',function () {//绑定邮箱或手机号
       var _this = $(this);
       var type = _this.data("type");
       var id = _this.data("id");
       if(type=='mobile'){
           window.location.href = baseloaction +'/mobile/uc/binding/bindingMobilePage';
       }
       if(type=='email'){
           window.location.href = baseloaction +'/mobile/uc/binding/bindingEmailPage';
       }
   }).on('click','.myAcademic',function () {//设置用户的学业
       window.location.href = baseloaction +'/mobile/uc/academic/myAcademic';
   }).on('click','.updateUserName',function () {//设置用户昵称
       var nickname = $(this).data('id');
       window.location.href=baseloaction+"/mobile/uc/userInfo/updateUserNickname?nickname="+nickname;
   }).on('click','.updateUserPassword',function () {//设置用户密码
       window.location.href=baseloaction+"/mobile/uc/userInfo/updateUserPassword";
   }).on('click','.updateUserInfo',function () {//设置用户个性签名
       var userInfo = $(this).data('info');
       if(userInfo==undefined){
           userInfo ='';
       }
       window.location.href=baseloaction+"/mobile/uc/userInfo/updateUserSignature?userInfo="+userInfo;
   }).on('click','.updateRealName',function () {//设置用户真实姓名
       var realName = $(this).data('id');
       if(realName ==null || realName == 'undefined'){
           realName = '';
       }
       window.location.href=baseloaction+"/mobile/uc/userInfo/updateUserRealName?realName="+realName;
   }).on('change','#updateUserGender',function () {//设置用户真实姓名
       var that  = $(this);
       Zepto.confirm("确认修改性别吗？", function () {
           var params = {"gender":that.val()};
           updataInfo(params)
       });
       Zepto('.modal-in').find('.modal-button').addClass('optCancel');
    }).on('click','.optCancel',function(){
         Zepto(".opt").prop("selected",true);
   }).on('click','.birthdayUpData',function(){
        var date =$('#birthdayDate').val().trim();
       if(date.split("-").length<3){
           $(".modal-overlay-visible").remove();
           $.toast('日期格式不正确，请选择完整的日期');
           return;
       }
       updataInfo({"birthday":date})
   }).on("click",".modal-overlay-visible",function(){
       if(initYear!=""&&initMoth!=""&&initDay!=""){
           $("#birthdayDate").val(initYear+"-"+initMoth+"-"+initDay);
       }else{
           $("#birthdayDate").val("填写生日有惊喜哦~~");
       }

       $(".modal-overlay-visible").remove();
   }).on("click","#birthdayDate",function(){
       if( $(this).is(":disabled")){
           $.toast('生日不可进行二次修改');
           return;
       }
       $("body").append("<div class=\"modal-overlay modal-overlay-visible\"></div>");
   }).on('click', '.myAddress', function () { //设置收货地址
       window.location.href=baseloaction+"/mobile/uc/userInfo/updateUserAddress";
   });
    var initYear = "";
    var initMoth= "";
    var initDay = "";
    initDate();
    function initDate(){
       var content= $("#birthdayDate").val();
        var split = content.split("-");
        if(split.length==3){
            initYear =split[0].trim();
            initMoth =split[1].trim();
            initDay =split[2].trim();
        }

    }

    var getDays = function(max) {
        var days = [];
        for(var i=1; i<= (max||31);i++) {
            days.push(i < 10 ? "0"+i : i);
        }
        return days;
    };

    var initMonthes = ('01 02 03 04 05 06 07 08 09 10 11 12').split(' ');

    var initYears = (function () {
        var arr = [];
        for (var i = 1980; i <= new Date().getFullYear()+5; i++) { arr.push(i); }
        return arr;
    })();
    var getDaysByMonthAndYear = function(month, year) {
        var int_d = new Date(year, parseInt(month)+1-1, 1);
        var d = new Date(int_d - 1);
        return getDays(d.getDate());
    };
    $('#birthdayDate').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
                                      <button class="button button-link pull-right close-picker birthdayUpData">确定</button>\
                                      <h1 class="title">请选择日期</h1>\
                                      </header>',
        cols: [
            {
                textAlign: 'center',
                values: initYears,
                cssClass: "col-province"
            },
            {
                textAlign: 'center',
                values: initMonthes,
                cssClass: "col-city"
            },
            {
                textAlign: 'center',
                values: getDays(),
                cssClass: "col-district"
            }
        ],
        formatValue: function (p, values, displayValues) {
            return displayValues[0] + '-' + values[1] + '-' + values[2];
        },
        onChange: function (picker, values, displayValues) {
            var days = getDaysByMonthAndYear(picker.cols[1].value, picker.cols[0].value);
            var currentValue = picker.cols[2].value;
            if(currentValue > days.length) currentValue = days.length;
            picker.cols[2].setValue(currentValue);
        },
        value: [initYear,initMoth,initDay]
    });
    //修改用户信息
    function updataInfo(params){
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/mobile/uc/userInfo/updateUserInfo',
            type: 'post',
            dataType: 'json',
            data: params,
            async: false,
            contentType:'application/json;charset=utf-8',
            success: function (result) {
                if(result.success){
                    $.alert(result.message,function () {
                        window.location.href=baseloaction+'/mobile/uc/userInfo';
                    });
                }else {
                    $.toast(result.message);
                }
            }
        })
    }
});